package the.offer.CodingInterview;

public class Charpter3 {

    /**
     * 16. 数值的整数次方
     */
    public static double Power(double base, int exponent) {
        if (exponent < 0)
            return 1 / PowerWithUnsignedExponent(base, -exponent);
        else
            return PowerWithUnsignedExponent(base, exponent);
    }

    private static double PowerWithUnsignedExponent(double base, int exponent) {
        if (exponent == 0)
            return 1;
        if (exponent == 1)
            return base;
        double result = PowerWithUnsignedExponent(base, exponent >> 1);
        result *= result;
        if ((exponent & 0x1) == 1)
            result *= base;
        return result;
    }


    public static void main(String[] args) {
        double power = Power(0, 0);
        System.out.println(power);
    }
}
